package com.xoms.front.persist.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.xoms.front.model.ProductBean;
import com.xoms.front.model.vo.ProductDetailBean;
import com.xoms.front.model.vo.TreeNodeBean;
import com.xoms.front.persist.dao.ProductDao;
import com.xoms.util.StringUtil;

@Repository
public class ProductImpl extends BaseImpl implements ProductDao {

	final String NAME_SPACE = NAME_SPACE_HEADER + ".ProductMapper";

	@Override
	public int selectCount(String key, String cid, String type) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		if (!StringUtil.isEmpty(cid)) {
			params.put("cid", cid);
		}
		if (!StringUtil.isEmpty(type)) {
			params.put("type", type);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectCount", params);
	}

	@Override
	public List<ProductBean> selectList(String key, boolean isPage, int page, int pagesize, String cid, String type) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("isPage", isPage);
		params.put("start", page * pagesize);
		params.put("length", pagesize);
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		if (!StringUtil.isEmpty(cid)) {
			params.put("cid", cid);
		}
		if (!StringUtil.isEmpty(type)) {
			params.put("type", type);
		}
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectList", params);
	}

	@Override
	public int selectProCountByName(String name) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectProCountByName", name);
	}

	@Override
	public ProductDetailBean selectOneById(String pid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectOneById", pid);
	}

	@Override
	public int selectGroupCount(String productId) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!StringUtil.isEmpty(productId)) {
			params.put("productId", productId);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectGroupCount", params);
	}

	@Override
	public int selectProCountByNameBar(String name, String productBar) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!StringUtil.isEmpty(name)) {
			params.put("name", name);
		}
		if (!StringUtil.isEmpty(productBar)) {
			params.put("productBar", productBar);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectProCountByNameBar", params);

	}

	@Override
	public List<ProductBean> selectProductByNameBar(String name, String productBar) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!StringUtil.isEmpty(name)) {
			params.put("name", name);
		}
		if (!StringUtil.isEmpty(productBar)) {
			params.put("productBar", productBar);
		}
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectProductByNameBar", params);

	}

	@Override
	public int selectCountByProBar(String productBar) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectCountByProBar", productBar);
	}

	@Override
	public String selectMaxProNo() {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectMaxProNo");
	}

	@Override
	public List<TreeNodeBean> selectSellProducts() {
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectSellProducts");
	}

	@Override
	public List<TreeNodeBean> selectSelectProducts(int type) {
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectSelectProducts", type);
	}

	@Override
	public List<ProductBean> selectListByStoreHouseId(String key, boolean isPage, int page, int pagesize, String cid,
			String type, String storeHouseId, Integer proStatus) {
		Map<String, Object> where = new HashMap<>();
		where.put("storeHouseId", storeHouseId);
		where.put("isPage", false);
		where.put("start", page * pagesize);
		where.put("length", pagesize);
		if (!StringUtil.isEmpty(key)) {
			where.put("key", key);
		}
		if (!StringUtil.isEmpty(cid)) {
			where.put("cid", cid);
		}
		if (!StringUtil.isEmpty(type)) {
			where.put("type", type);
		}
		where.put("proStatus", proStatus);
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectListByStoreHouseId", where);
	}

	@Override
	public List<ProductBean> selectListByPids(List<String> pids) {
		if (pids == null || pids.size() == 0) {
			return null;
		}
		Map<String, Object> where = new HashMap<>();
		where.put("pids", pids);
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectListByPids", where);
	}

}
